<template>
  <div class="app-container">
    <div v-if="Active">
      <!-- <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findData1" @Launchform="Launchform" /> -->
      <Find
        :OriginalFilter="{ static: true }"
        :searchImg="false"
        @findClicked="findData"
        :formList="formList1"
        @Launchform="Launchform1"
        @clickNumber="clickNumber1"
        @clickNumber1="clickNumber2"
        @handleBlur="selectVal"
      />

      <!-- <div>
      <el-card shadow="never" :body-style="{ padding: '5px' }">
        <div>
          <span style="font-weight: 600;">指令明细</span>
        </div>
        <Table @currentRow="clicktogetdata" :isLoading="isLoading" :tableHeader="tableHeader"
          :tableData="LotNoQueryMethodsTableDateList" selection :TableHeight="47" :outTableHeight="90"></Table>
        <PageModule :PageData="{ buttonS: true, total: total }" @pageChange="pageChange"
          @pageSizeChange="pageSizeChange" />
      </el-card>

    </div> -->

      <div>
        <el-card shadow="never" :body-style="{ padding: '5px' }">
          <div>
            <span style="font-weight: 600">{{ $t(`Print.AggregateMaterialInquiry`) }}</span>
          </div>
          <Table
            @selectList="selectList"
            :tableHeader="tableHeader2"
            :tableData="SummaryMateriaParmsMethodsTableDateList"
            selection
            :TableHeight="100"
            @currentRow="currentRow3"
          ></Table>
          <PageModule :PageData="{ buttonS: true, total: totalList }" @pageChange="pageChange1" @pageSizeChange="pageSizeChange1" />
        </el-card>
      </div>
    </div>

    <div v-if="!Active">
      <Find @clickNumber="clickNumber" @findClicked="findData7" @Launchform="Launchform7" :searchImg="false" :formList="formList7" />
      <el-card style="margin-top: 20px" shadow="never" :body-style="{ padding: '5px' }">
        <TangTable
          @click="dlsopa"
          @showContextMenu="showContextMenu"
          @selectList="selectList2"
          selection
          @currentRow1="clicktogetdata3"
          :isLoading="isLoading2"
          @inputChange="inputChange"
          @currentRow="clicktogetdata1"
          class="divNitive"
          :tableHeader="tableHeader1"
          :tableData="OrderingProgramQuerTableDate"
          :TableHeight="100"
        />
        <PageModule :PageData="{ buttonS: true, total: total1 }" @pageChange="pageChange11" @pageSizeChange="pageSizeChange11" />
      </el-card>
    </div>

    <div v-if="showMenu" :style="{ left: `${menuPosition.x - 170}px`, top: `${menuPosition.y}px` }" class="custom-context-menu">
      <ul>
        <li @click="downMenu">{{ $t(`Print.CloseTheMenu`) }}</li>
        <li @click="EmptyOrderingInformationMethods">{{ $t(`Print.ResetPurchaseInfo`) }}</li>
        <li @click="bankMethods">{{ $t(`Print.ReturnAction`) }}</li>
        <li>{{ $t(`Print.CopyTheManufacturersDeliveryDate`) }}</li>
        <li @click="SingleCancelPurchaseOrderMethods">{{ $t(`Print.SingleLinePurchaseOrderCancellations`) }}</li>
        <li>{{ $t(`Print.DefinitionOfCodeClassification`) }}</li>
        <li @click="cancel">{{ $t(`Print.SubscriptionCancellation`) }}</li>
      </ul>
    </div>

    <el-dialog v-model="dialogVisible8" width="50%">
      <Find :OriginalFilter="{ static: true }" @findClicked="findData17" @Launchform="Launchform17" :searchImg="false" :formList="formList15" />
    </el-dialog>

    <el-dialog width="30%" v-model="dialogVisible" :title="$t(`Print.CommandSelection`)">
      <qiTable
        @currentRow1="currentRowList2"
        :tableHeader="tableHeader7"
        :tableData="tableHeader7TableDate"
        :TableHeight="50"
        :outTableHeight="150"
      />
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, onUnmounted } from 'vue'
import { classifyQuery } from '@/api/WarehouseDepartment/FactoryStorage/FitOutsourcingProcessing'
import { ElMessage } from 'element-plus'
import { GainVendor } from '@/api/WarehouseDepartment/FactoryStorage/ConsumablesIn'
import LoginWork from '@/utils/LoginWork'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
import { clearDate } from '@/api/PurchasingDepartment/FactoryPurchasing/MaterialSummaryTl'
import { JudgmentInstruction } from '@/api/WarehouseDepartment/FactoryStorage/InputInsoleReceivingIssuingMaterials'
export default defineComponent({
  name: 'MaterialRequisition'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ref, watch } from 'vue'
import {
  batchModifying,
  BatchReturn,
  lotNoQuery,
  bank,
  OrderCancellation,
  EmptyOrderingInformation,
  SingleCancelPurchaseOrder,
  QueryAll,
  BatchProcessingOfPurchaseRequests,
  classify,
  LotNoQuery,
  SummaryMaterialDetail,
  OrderingProgramQuery,
  FormAPurchaseOrder,
  ManufacturerQuery
} from '@/api/PurchasingDepartment/FactoryPurchasing/MaterialRequisition'
import { lotNoQuery1 } from '@/api/WarehouseDepartment/FactoryStorage/InputInsoleReceivingIssuingMaterials'
import type { LotNoQueryParms, SummaryMateriaParms } from '@/api/PurchasingDepartment/FactoryPurchasing/MaterialRequisition'
import { ElLoading, ElMessageBox } from 'element-plus'
const tableHeader7 = ref([
  {
    label: '指令',
    prop: 'lotNo',
    width: '160'
  }
])
const tableHeader7TableDate = ref([])
// 批量修改厂商
const batchModifyingParms = ref({
  list: [],
  vendorName: '',
  vendorNo: ''
})

const currentRowList2 = (row: any) => {
  formList1.value[0].inputValue = row.lotNo
  dialogVisible.value = false
}

const selectVal = (row: any) => {
  if(SummaryMaterialDetailMethodsParms.value.lotNo !== ''){
    lotNoList1.value = row
    JudgmentInstructionMethods()
  }
 
}
const dialogVisible = ref(false)
const JudgmentInstructionMethods = async () => {
  if (lotNoList1.value !== '') {
    const result = await JudgmentInstruction(lotNoList1.value)
    if (result.code === 200) {
    } else {
      lotNoQueryMethods1()
      dialogVisible.value = true
    }
  }
}
const lotNoList1 = ref('')
const lotNoQueryMethods1 = async () => {
  const result = await lotNoQuery1(SummaryMaterialDetailMethodsParms.value.lotNo)
  tableHeader7TableDate.value = result
.map((item: any) => {
    return { lotNo: item }
  })
}

const batchModifyingMethods = async () => {
  try {
    const result = await batchModifying(batchModifyingParms.value)
    if (result.code === 200) {
      OrderingProgramQueryMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('批量修改失败请重试')
  }
}

const Active = ref(true)

// 批量退回
const BatchReturnMethodsParms = ref([])

const selectList1 = (row: any) => {}

const BatchReturnMethods = async () => {
  try {
    const result = await BatchReturn(BatchReturnMethodsParms.value)
    if (result.code === 200) {
      OrderingProgramQueryMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('批量退回出错请重试')
  }
}

// 指令查询
const lotNoQueryMethods = async () => {
  try {
    const result = await lotNoQuery()
    formList1.value[0].selectOptions = EvaluatesTheArrayReturnValue(result.lotNoList)
    formList1.value[0].selectValue = formList1.value[0].selectOptions[0].optionValue
    SummaryMaterialDetailMethodsParms.value.lotNo = formList1.value[0].selectOptions[0].optionValue
    SummaryMateriaParmsMethods()
  } catch (error) {
    JMessage.error('指令查询出错')
  }
}

// 订购取消
const OrderCancellationParms = ref()

const purNoList = ref('')

const Launchform17 = (row: any) => {
  OrderCancellationParms.value.qty2 = row[11].inputValue
  OrderCancellationParms.value.minusQty2 = row[12].inputValue
  row[13].inputValue = OrderCancellationParms.value.qty2
}

const showContextMenu = (event: any, row: any) => {
  formList15.value[0].inputValue = row.mtlColor
  formList15.value[1].inputValue = row.mtlName
  formList15.value[2].inputValue = row.mtlUnit
  formList15.value[3].inputValue = row.purNo

  formList15.value[4].inputValue = row.qty
  formList15.value[5].inputValue = row.minusQty
  formList15.value[6].inputValue = row.purQty

  formList15.value[7].inputValue = row.purQty

  formList15.value[8].inputValue = row.purDate

  SingleCancelPurchaseOrderParms.value = row
  EmptyOrderingInformationrParms.value = row
  OrderCancellationParms.value = row
  bankParms.value = row
  showMenu.value = false
  showMenu.value = true
  menuPosition.value.x = event.pageX
  menuPosition.value.y = event.pageY
}

const findData17 = (row: string) => {
  if (row === '取消处理') {
    if (OrderCancellationParms.value.qty < OrderCancellationParms.value.qty2) {
      return JMessage.warning('不能超过订单数量')
    }
    OrderCancellationParms.value.function = 1
    OrderCancellationMethods()
  }

  if (row === '取消还原处理') {
    if (OrderCancellationParms.value.minusQty < OrderCancellationParms.value.minusQty2) {
      return JMessage.warning('不能超过多购量')
    }
    OrderCancellationParms.value.function = 2
    OrderCancellationMethods()
  }
}

const OrderCancellationMethods = async () => {
  showMenu.value = false
  try {
    const result = await OrderCancellation(OrderCancellationParms.value)
    if (result.code === 200) {
      OrderingProgramQueryMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('订购取消失败请重试')
  }
}

const findData15 = (row: string) => {
  if (row === '返回请购') {
    showMenu.value = false
  }
}

// 退回
const dialogVisible8 = ref(false)

const cancel = () => {
  if (OrderCancellationParms.value.purNo === '' || OrderCancellationParms.value.purNo === null) {
    showMenu.value = false
    JMessage.warning('请选择已下单的进行取消订购')
    return
  }
  showMenu.value = false
  dialogVisible8.value = true
}

const formList15 = ref([
  {
    type: 'input',
    inputPlaceholder: '材料颜色',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '单位',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '订购单号',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '正单量',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '多购量',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '订购量',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '回厂数量',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '订购日期',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '已取消数量',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '可取消数量',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '正单量',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '多购量',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input1',
    inputPlaceholder: '',
    inputValue: '',
    inputLabel: '',
    inputDisabled: true
  },
  {
    type: 'button',
    checkboxLabel: '取消处理'
  },
  {
    type: 'button',
    checkboxLabel: '取消还原处理'
  },
  {
    type: 'button',
    checkboxLabel: '返回请购'
  }
])

// 置空订购消息
const bankParms = ref()

const bankMethods = async () => {
  showMenu.value = false
  try {
    const result = await bank(bankParms.value)
    if (result.code === 200) {
      OrderingProgramQueryMethods()

      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('单行采购单取消失败')
  }
}

// 置空订购消息
const EmptyOrderingInformationrParms = ref()

const EmptyOrderingInformationMethods = async () => {
  showMenu.value = false
  try {
    const result = await EmptyOrderingInformation(EmptyOrderingInformationrParms.value)
    if (result.code === 200) {
      OrderingProgramQueryMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('单行采购单取消失败')
  }
}

const SingleCancelPurchaseOrderParms = ref()

const SingleCancelPurchaseOrderMethods = async () => {
  showMenu.value = false
  try {
    const result = await SingleCancelPurchaseOrder(SingleCancelPurchaseOrderParms.value)
    if (result.code === 200) {
      OrderingProgramQueryMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('单行采购单取消失败')
  }
}

const menuPosition = ref({ x: 0, y: 0 })

const dlsopa = () => {
}

const downMenu = () => {
  showMenu.value = false
}

const showMenu = ref(false)

const openFullScreen2 = () => {
  const loading = ElLoading.service({
    lock: true,
    text: '正在汇总请稍后',
    background: 'rgba(0, 0, 0, 0.7)'
  })
  return loading
}

// 请购批处理
const BatchProcessingOfPurchaseRequestsPrams = ref([])

const BatchProcessingOfPurchaseRequestsPrams1 = ref<any>({})

const selectList = (row: any) => {
  BatchProcessingOfPurchaseRequestsPrams.value = row.map((item: any) => {
    return item
  })
  lotNoList.value = row.map((item: any) => {
    return item.lotNo
  })
}

const BatchProcessingOfPurchaseRequestsMethdos = async () => {
  const result = await BatchProcessingOfPurchaseRequests(
    BatchProcessingOfPurchaseRequestsPrams.value.map((item: any) => {
      item.custom = BatchProcessingOfPurchaseRequestsPrams1.value.custom
      item.style = BatchProcessingOfPurchaseRequestsPrams1.value.style
      return item
    })
  )
  let active = openFullScreen2()
  if (result.code === 200) {
    SummaryMateriaParmsMethods()
    active.close()
    JMessage.success(result.message)
  } else {
    active.close()
    JMessage.error(result.message)
  }
}

const selectList2 = (row: any) => {
  FormAPurchaseOrderParms.value.request = row.map((item: any) => {
    return item
  })
  BatchReturnMethodsParms.value = row.map((item: any) => {
    return item
  })
  batchModifyingParms.value.list = row.map((item: any) => {
    return item.id
  })
}

// 形成订购单
const FormAPurchaseOrderParms = ref<any>({
  request: [],
  requiredDate: '',
  vendorName: '',
  vendorNo: ''
})

const clicktogetdata3 = (row: any) => {
  if (listVendorList.value !== '') {
    row.vendorNo = listVendorList.value.split(' ')[0]
    row.vendorName = listVendorList.value.split(' ')[1]
    inputChange(row)
  }
}

const isLoading2 = ref(true)

const inputChange = async (row: any) => {
  const result = await LoginWork.post<any, any>('/MaterialClearance/updatePurchaseOrder2', row)
  if (result.code === 200) {
    OrderingProgramQueryMethods()
    JMessage.success(result.message)
  } else {
    OrderingProgramQueryMethods()
    JMessage.error(result.message)
  }
}

const FormAPurchaseOrderMethods = async () => {
  const result = await FormAPurchaseOrder(FormAPurchaseOrderParms.value)
  if (result.code === 200) {
    FormAPurchaseOrderParms.value.request = []
    OrderingProgramQueryMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const purFlagList = ref('')

const clicktogetdata1 = (row: any) => {
  purFlagList.value = row.purFlag
  purNoList.value = row.purNo
}

const dialogVisible6 = ref(false)

const findData7 = async (row: string) => {
  if (row === '批量修改厂商') {
    if (batchModifyingParms.value.list.length === 0) {
      JMessage.warning('请勾选需要修改的数据')
    } else if (batchModifyingParms.value.vendorName === '') {
      JMessage.warning('厂商不可为空')
    } else {
      handleCloseMateriaLKlcLtvCodeUsage2()
    }
  }
  if (row === '搜索') {
    OrderingProgramQueryMethods()
  }
  if (row === '批量退单') {
    if (BatchReturnMethodsParms.value.length === 0) return JMessage.warning('请勾选需要退回的数据')
    handleCloseMateriaLKlcLtvCodeUsage()
  }
  if (row === '返回') {
    SummaryMateriaParmsMethods()
    Active.value = true
  }

  if (row === '置空') {
    claerInputValueAndSelectValue2()
  }

  if (row === '形成订购单') {
    if (FormAPurchaseOrderParms.value.request.length === 0) {
      JMessage.warning('请选择需要形成订购单的数据')
    } else if (FormAPurchaseOrderParms.value.requiredDate === '') {
      JMessage.warning('需求日期不可为空')
    } else if (FormAPurchaseOrderParms.value.vendorName === '') {
      JMessage.warning('厂商不可为空')
    } else {
      FormAPurchaseOrderMethods()
    }
  }

  if (row === '导出订购单') {
    OrderingProgramQueryParms.value.purNo = purNoList.value
    if (OrderingProgramQueryParms.value.purNo === '' || OrderingProgramQueryParms.value.purNo === null) {
      JMessage.warning('请点击需要导出的订购单号')
    } else {
      const res = (await fileRequest.get(`/MaterialClearance/exportPurchaseOrder?purNo=${OrderingProgramQueryParms.value.purNo}`)) as any
      const link = document.createElement('a') // 利用a标签自定义下载文件名
      let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
      link.style.display = 'none'
      //设置连接
      link.href = window.URL.createObjectURL(blob)
      const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
      link.download = FileName //自定义文件名字
      document.body.appendChild(link)
      //模拟点击事件
      link.click()
      document.body.removeChild(link)
    }
    OrderingProgramQueryParms.value.purNo = ''
  }

  if (row === '导出分码订购单') {
    OrderingProgramQueryParms.value.purNo = purNoList.value
    if (OrderingProgramQueryParms.value.purNo === '') {
      JMessage.warning('订购单号不可为空')
    } else {
      const res = (await fileRequest.get(`/MaterialClearance/exportSziePurchaseOrder?purNo=${OrderingProgramQueryParms.value.purNo}`)) as any
      const link = document.createElement('a') // 利用a标签自定义下载文件名
      let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
      link.style.display = 'none'
      //设置连接
      link.href = window.URL.createObjectURL(blob)
      const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
      link.download = FileName //自定义文件名字
      document.body.appendChild(link)
      //模拟点击事件
      link.click()
      document.body.removeChild(link)
    }
  }
  OrderingProgramQueryParms.value.purNo = ''
}

const handleCloseMateriaLKlcLtvCodeUsage2 = (_done?: () => void) => {
  ElMessageBox.confirm('确认要批量修改厂商数据吗?')
    .then(async () => {
      batchModifyingMethods()
    })
    .catch(() => {})
}

const handleCloseMateriaLKlcLtvCodeUsage = (_done?: () => void) => {
  ElMessageBox.confirm('确认要批量退单吗?')
    .then(async () => {
      BatchReturnMethods()
    })
    .catch(() => {})
}

const claerInputValueAndSelectValue2 = () => {
  formList7.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'date') {
      item.dateValue = ''
    }
    if (item.type === 'dateSingle') {
      item.dateValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
    if (item.type === 'selectJ') {
      item.selectValue = ''
    }
  })
}

const Launchform7 = (row: any) => {
  OrderingProgramQueryParms.value.purNo = row[0].inputValue
  OrderingProgramQueryParms.value.reqNo = row[1].inputValue
  OrderingProgramQueryParms.value.lotDetail = row[2].inputValue
  OrderingProgramQueryParms.value.purType = row[3].selectValue
  OrderingProgramQueryParms.value.typeNo = row[4].selectValue.split(' ')[0]

  OrderingProgramQueryParms.value.vendorNo = row[5].selectValue.split(' ')[0]
  OrderingProgramQueryParms.value.mtlName = row[6].inputValue
  OrderingProgramQueryParms.value.sysUser = row[7].inputValue

  OrderingProgramQueryParms.value.purDateEnd = row[8].dateValue[1]
  OrderingProgramQueryParms.value.purDateStart = row[8].dateValue[0]

  FormAPurchaseOrderParms.value.requiredDate = row[17].dateValue
  FormAPurchaseOrderParms.value.vendorName = row[16].selectValue.split(' ')[1]
  listVendorList.value = row[16].selectValue
  FormAPurchaseOrderParms.value.vendorNo = row[16].selectValue.split(' ')[0]

  batchModifyingParms.value.vendorName = row[16].selectValue.split(' ')[1]
  batchModifyingParms.value.vendorNo = row[16].selectValue.split(' ')[0]

  if (row[12].checked) {
    OrderingProgramQueryParms.value.userFlag = 2
    // OrderingProgramQueryMethods()
  } else {
    OrderingProgramQueryParms.value.userFlag = 0
    //   OrderingProgramQueryMethods()
  }
}

const listVendorList = ref('')

const clickNumber = (row: string) => {
  switch (row) {
    case '1':
      OrderingProgramQueryParms.value.purFlag = '1'
      break
    case '0':
      OrderingProgramQueryParms.value.purFlag = '2'
      break
    case '2':
      OrderingProgramQueryParms.value.purFlag = '0'
      break
  }
  OrderingProgramQueryMethods()
}

// 查询
const OrderingProgramQueryParms = ref({
  lotDetail: '',
  pageNum: 1,
  pageSize: 40,
  purDateEnd: '',
  purDateStart: '',
  purFlag: '1',
  purNo: '',
  purType: '',
  typeNo: '',
  vendorNo: '',
  reqNo: '',
  userFlag: 0,
  sysUser: '',
  mtlName: ''
})

// const OrderingProgramQueryMethods1 = async () => {
//   isLoading2.value = true
//   const result = await QueryAll(OrderingProgramQueryParms.value)
//   OrderingProgramQuerTableDate.value = result.lotNoList.map((item: any) => {
//     if (item.purFlag !== 'Y') {
//       item.change = 'Y'
//       item.disableConfirm = true
//     }
//     return item
//   })
//   total1.value = result.total
//   isLoading2.value = false
// }

const OrderingProgramQuerTableDate = ref([])

const OrderingProgramQueryMethods = async () => {
  isLoading2.value = true
  const result = await OrderingProgramQuery(OrderingProgramQueryParms.value)
  OrderingProgramQuerTableDate.value = result.lotNoList.map((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
    item.lsitanyButton = true
    return item
  })
  total1.value = result.total
  isLoading2.value = false
}

const classifyQueryMethods = async () => {
  let NewArray = []
  const result = await classifyQuery()
  NewArray = result.reduce((arr: any, item: any) => {
    if (item.typeNo && item.typeName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.typeNo + ' ' + item.typeName)
    }
    return arr
  }, [])
  formList7.value[4].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
}

const total1 = ref(0)

const pageChange11 = (val: any) => {
  OrderingProgramQueryParms.value.pageNum = val
  OrderingProgramQueryMethods()
}

const pageSizeChange11 = (val: any) => {
  OrderingProgramQueryParms.value.pageSize = val
  OrderingProgramQueryMethods()
}

const disableActive = ref(true)

const formList7 = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入订购单号',
    inputValue: '',
    inputLabel: '',
    inputWidth: '150px'
  },
  {
    type: 'input',
    inputPlaceholder: '请购单号',
    inputValue: '',
    inputLabel: '',
    inputWidth: '150px'
  },
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: '',
    inputLabel: '',
    inputWidth: '150px'
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '类别',
    selectLabel: '',
    selectOptions: [
      { optionKey: 0, optionValue: '汇总', optionLabel: '汇总' },
      { optionKey: 1, optionValue: '零星', optionLabel: '零星' },
      { optionKey: 2, optionValue: '补料', optionLabel: '补料' },
      { optionKey: 3, optionValue: '五金', optionLabel: '五金' },
      { optionKey: 4, optionValue: '差异', optionLabel: '差异' }
    ]
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '分类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'selectJ',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称',
    inputValue: '',
    inputLabel: '',
    inputWidth: '150px'
  },
  {
    type: 'input',
    inputPlaceholder: '采购员',
    inputValue: '',
    inputLabel: '',
    inputWidth: '150px'
  },
  {
    type: 'date',
    dateLabel: '订购日期',
    dateValue: ''
  },
  {
    type: 'radio',
    checkboxLabel: '未下单',
    checked: true,
    label: '1'
  },
  {
    type: 'radio',
    checkboxLabel: '已下单',
    checked: false,
    label: '0'
  },
  {
    type: 'radio',
    checkboxLabel: '全部',
    checked: false,
    label: '2'
  },
  {
    type: 'checkbox',
    checkboxLabel: '全部订购单',
    checked: false,
    disabled: disableActive
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '搜索',
    bgColor: '#1890FF'
  },

  {
    type: 'button',
    checkboxLabel: '返回'
  },

  {
    type: 'selectJ',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'dateSingle',
    dateLabel: '需求日期',
    dateValue: ''
  },

  {
    type: 'button',
    checkboxLabel: '形成订购单'
  },
  {
    type: 'button',
    checkboxLabel: '导出订购单'
  },
  {
    type: 'button',
    checkboxLabel: '导出分码订购单'
  },
  {
    type: 'button',
    checkboxLabel: '批量修改厂商',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '批量退单',
    bgColor: '#1890FF'
  }
])

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入品牌',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'date',
    dateLabel: '下单日期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '客人交期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '生控计划',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '搜索'
  }
])

const formList1 = ref<any>([
  // {
  //   type: 'select',
  //   selectValue: '',
  //   selectP: '指令',
  //   selectLabel: '',
  //   selectOptions: []
  // },
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '类型',
    selectLabel: '',
    selectOptions: [
      {
        optionKey: '汇总',
        optionValue: '汇总',
        optionLabel: '汇总'
      },
      {
        optionKey: '差异',
        optionValue: '差异',
        optionLabel: '差异'
      }
    ]
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料名称',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料颜色',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '分类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'radio',
    checkboxLabel: '隐藏',
    checked: true,
    label: '2'
  },
  {
    type: 'radio',
    checkboxLabel: '未隐藏',
    checked: false,
    label: '0'
  },

  {
    type: 'radio',
    checkboxLabel: '全部',
    checked: false,
    label: '1'
  },
  {
    type: 'button',
    checkboxLabel: '请购批处理'
  },
  {
    type: 'button',
    checkboxLabel: '进入订购程序',
    bgColor: '#1890FF'
  },

  {
    type: 'radio1',
    checkboxLabel: '正数',
    checked: true,
    label: '2'
  },
  {
    type: 'radio1',
    checkboxLabel: '负数',
    checked: false,
    label: '1'
  },
  {
    type: 'radio1',
    checkboxLabel: '正+负数',
    checked: false,
    label: '3'
  },
  {
    type: 'radio1',
    checkboxLabel: '0',
    checked: false,
    label: '4'
  },
  {
    type: 'radio1',
    checkboxLabel: '全部',
    checked: false,
    label: '0'
  },

  {
    type: 'button',
    checkboxLabel: '搜索',
    bgColor: '#1890FF'
  },
    {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '清洗数据',
    bgColor: '#1890FF'
  }
])

const EvaluatesTheArrayReturnValue1 = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      value: item,
      label: item
    }
  })
}

const GainVendorList = ref([])
const VendorQueryMethods = async () => {
  let NewArray: never[] = []
  const result = await ManufacturerQuery()
  NewArray = result.reduce((arr: any, item: any) => {
    if (item.vendorNo && item.vendorName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.vendorNo + ' ' + item.vendorName)
    }
    return arr
  }, [])
  formList7.value[5].selectOptions = EvaluatesTheArrayReturnValue3(NewArray)
  formList7.value[16].selectOptions = EvaluatesTheArrayReturnValue3(NewArray)
  GainVendorList.value = EvaluatesTheArrayReturnValue1(NewArray)
}

const EvaluatesTheArrayReturnValue3 = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      value: item,
      label: item
    }
  })
}

const findData1 = (count: string) => {
  if (count === '置空') {
    claerInputValueAndSelectValue1()
  }

  if (count === '搜索') {
    LotNoQueryMethods()
  }
}
// 清晰数据
const lotNoList = ref([])
const clearDateMethods = async () => {
  if (lotNoList.value.length === 0) {
    JMessage.warning('请勾选需要清洗的数据')
    return
  }
  const result = await clearDate(lotNoList.value)
  if (result.code === 200) {
    SummaryMateriaParmsMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}
const currentRow3 = (row:any) => {
  // lotNoList.value = row.lotNo
}
const findData = (count: string) => {
  if (count === '清洗数据') {
    clearDateMethods() 
  }
  if (count === '搜索') {
    SummaryMateriaParmsMethods()
  }
  if (count === '置空') {
    LotNoQueryMethods()
    claerInputValueAndSelectValue()
  }
  if (count === '进入订购程序') {
    // OrderingProgramQueryMethods()
    Active.value = false
    VendorQueryMethods()
    classifyQueryMethods()
    OrderingProgramQueryMethods()
    dialogVisible6.value = true
  }
  if (count === '请购批处理') {
    if (BatchProcessingOfPurchaseRequestsPrams.value.length === 0) {
      JMessage.warning('请勾选需要请购的数据')
    } else {
      BatchProcessingOfPurchaseRequestsMethdos()
    }
  }
}

const claerInputValueAndSelectValue = () => {
  formList1.value.forEach((item: any) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
  })
}

const claerInputValueAndSelectValue1 = () => {
  formList.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'date') {
      item.dateValue = ''
    }
  })
}

const clickNumber1 = (count: string) => {
  switch (count) {
    case '1':
      SummaryMaterialDetailMethodsParms.value.hideFlag = '0'
      break
    case '0':
      SummaryMaterialDetailMethodsParms.value.hideFlag = '1'
      break
    case '2':
      SummaryMaterialDetailMethodsParms.value.hideFlag = '2'
      break
    default:
      break
  }
  SummaryMateriaParmsMethods()
}

const clickNumber2 = (count: string) => {
  switch (count) {
    case '3':
      SummaryMaterialDetailMethodsParms.value.pluralFlag = '3'
      break
      case '4':
      SummaryMaterialDetailMethodsParms.value.pluralFlag = '4'
      break
    case '1':
      SummaryMaterialDetailMethodsParms.value.pluralFlag = '2'
      break
    case '0':
      SummaryMaterialDetailMethodsParms.value.pluralFlag = '0'
      break
    case '2':
      SummaryMaterialDetailMethodsParms.value.pluralFlag = '1'
      break
    default:
      break
  }
  SummaryMateriaParmsMethods()
}
// 汇总明细查询

const SummaryMaterialDetailMethodsParms = ref<any>({
  type: '',
  mtlName: '',
  mtlColor: '',
  hideFlag: '0',
  typeNo: '',
  lotNo: '',
  pageSize: 40,
  pageNum: 1,
  pluralFlag: '0'
})

const Launchform1 = (row: any) => {
  SummaryMaterialDetailMethodsParms.value.lotNo = row[0].inputValue
  SummaryMaterialDetailMethodsParms.value.type = row[1].selectValue
  SummaryMaterialDetailMethodsParms.value.mtlName = row[2].inputValue
  SummaryMaterialDetailMethodsParms.value.mtlColor = row[3].inputValue
  SummaryMaterialDetailMethodsParms.value.typeNo = row[4].selectValue.split(' ')[0]
}

const pageChange1 = (val: any) => {
  SummaryMaterialDetailMethodsParms.value.pageNum = val
  SummaryMateriaParmsMethods()
}

const pageSizeChange1 = (val: any) => {
  SummaryMaterialDetailMethodsParms.value.pageSize = val
  SummaryMateriaParmsMethods()
}

const clicktogetdata = (row: any) => {
  const { lotNo } = row
  SummaryMaterialDetailMethodsParms.value.lotNo = lotNo
  formList1.value[0].inputValue = lotNo
  BatchProcessingOfPurchaseRequestsPrams1.value = lotNo
  SummaryMateriaParmsMethods()
}

const totalList = ref()

const isLoading1 = ref(false)

const SummaryMateriaParmsMethodsTableDateList = ref<any[]>()

const SummaryMateriaParmsMethods = async () => {
  isLoading1.value = true
  const res = await SummaryMaterialDetail(SummaryMaterialDetailMethodsParms.value)
  if(res.code === 200){
    SummaryMateriaParmsMethodsTableDateList.value = res.data.lotNoList.map((item: any) => {
        if (item.type === '差异') {
          item.backgroundColor = 'red'
        }
        return item
      })
      // formList1.value[0].inputValue = res.data.lotNoList[0]
  totalList.value = res.data.total
  }else{
    ElMessage.error(res.message)
  }
 
  isLoading1.value = false
}

const isLoading = ref(false)

// 指令明细查询
const pageChange = (val: any) => {
  LotNoQueryMethodsParms.value.pageNum = val
  LotNoQueryMethods()
}

const pageSizeChange = (val: any) => {
  LotNoQueryMethodsParms.value.pageSize = val
  LotNoQueryMethods()
}

const LotNoQueryMethodsParms = ref<any>({
  lotNo: '',
  style: '',
  custom: '',
  orderDateStart: '',
  orderDateEnd: '',
  deliveryDateStart: '',
  deliveryDateEnd: '',
  pageSize: 40,
  pageNum: 1,
  upperPurPlanStart: '',
  upperPurPlanEnd: ''
})

const Launchform = (row: any) => {
  LotNoQueryMethodsParms.value.lotNo = row[0].inputValue
  LotNoQueryMethodsParms.value.style = row[1].inputValue
  LotNoQueryMethodsParms.value.custom = row[2].inputValue
  LotNoQueryMethodsParms.value.orderDateStart = row[3].dateValue[0]
  LotNoQueryMethodsParms.value.orderDateEnd = row[3].dateValue[1]
  LotNoQueryMethodsParms.value.deliveryDateStart = row[4].dateValue[0]
  LotNoQueryMethodsParms.value.deliveryDateEnd = row[4].dateValue[1]
  LotNoQueryMethodsParms.value.upperPurPlanStart = row[5].dateValue[0]
  LotNoQueryMethodsParms.value.upperPurPlanEnd = row[5].dateValue[1]
}

const LotNoQueryMethodsTableDateList = ref<any>([])

const total = ref()

const LotNoQueryMethods = async () => {
  isLoading.value = true
  const result = await LotNoQuery(LotNoQueryMethodsParms.value)
  LotNoQueryMethodsTableDateList.value = result.lotNoList
  imgList.value = result.lotNoList.map((item: any) => {
    return item.photoPath
  })
  if (result.lotNoList.length > 0) {
    // formList1.value[0].inputValue = result.lotNoList[0].lotNo
    // SummaryMaterialDetailMethodsParms.value.lotNo = result.lotNoList[0].lotNo
    // BatchProcessingOfPurchaseRequestsPrams1.value = result.lotNoList[0]
    // SummaryMateriaParmsMethods()
  }
  total.value = result.total
  isLoading.value = false
}

const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

// 分类获取
const classifyMethods = async () => {
  let NewArray = []
  const result = await classify()
  NewArray = result.reduce((arr: any, item: any) => {
    if (item.typeNo && item.typeName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.typeNo + ' ' + item.typeName)
    }
    return arr
  }, [])

  formList1.value[4].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
}

const imgList = ref([])

onMounted(() => {
  const token: any = JSON.parse(sessionStorage.getItem('userInfo')!)

  if (token.posName === '咨询顾问' || token.posName === '采购经理') {
    disableActive.value = false
  } else {
    disableActive.value = true
  }

  lotNoQueryMethods()

  classifyMethods()

  LotNoQueryMethods()
})

const tableHeader = ref([
  // {
  //   label: '图片', fixed: true,
  //   prop: 'photoPath',
  //   srcList: imgList
  // },
  {
    type: 'img',
    fixed: true,
    prop: 'photoPath',
    label: '图片',
    srcList: imgList
  },
  {
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '工厂型体',
    prop: 'style'
  },
  {
    label: '品牌',
    prop: 'custom'
  },
  {
    label: '订单数',
    prop: 'orderQty'
  },
  {
    label: '预补数',
    prop: 'surplusQty'
  },
  {
    label: '下单日期',
    prop: 'orderDate'
  },
  {
    label: '客人交期',
    prop: 'deliveryDate'
  },
  {
    label: '采购周期',
    prop: 'upperMtlLt'
  },
  {
    label: '生控计划',
    prop: 'upperPurPlan'
  }
])

const tableHeader2 = ref([
  {
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '类型',
    prop: 'type',
    backgroundColor: true
  },

      {
    label: '物料分类',
    prop: 'typeName'
  },
  {
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    start: true
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '需求量',
    prop: 'yqty'
  },
  {
    label: '已购量',
    prop: 'xqty'
  },
  {
    label: '待购量',
    prop: 'cyQty'
  },

])

const tableHeader1 = ref([
  {
    label: '类别',
    prop: 'purType',
    change: true
  },
  {
    label: '指令号',
    prop: 'lotDetail',
    change: true
  },
  {
    label: '物料编码',
    prop: 'mtlNo',
    change: true
  },
  {
    label: '颜色(英)',
    prop: 'engColor',
    change: true
  },
  {
    label: '材料颜色',
    prop: 'mtlColor',
    change: true
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    change: true,
    start: true
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    change: true
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    change: true
  },
  {
    label: '订购单号',
    prop: 'purNo',
    change: true
  },
  {
    label: '订购日期',
    prop: 'purDate'
  },
  {
    label: '正单量',
    prop: 'qty',
    change: true
  },
  {
    label: '预加量',
    prop: 'overQty'
  },
  {
    label: '多购量 ',
    prop: 'minusQty'
  },
  {
    label: '订购数量',
    prop: 'purQty',
    change: true
  },
  {
    label: '厂商代码',
    prop: 'vendorNo'
  },
  {
    label: '厂商名称',
    prop: 'vendorName'
  },
  {
    label: '需求交期',
    prop: 'vendorDate'
  },
  {
    label: '厂商回复交期',
    prop: 'inDate'
  },
  {
    label: '需求日期',
    prop: 'reqRta',
    change: true
  },
  {
    label: '请购单号',
    prop: 'reqNo',
    change: true
  },

  {
    label: '请购备注',
    prop: 'reqMemo',
    change: true
  },
  {
    label: '取消数量',
    prop: 'cancelQty',
    change: true
  },
  {
    label: '请购日期',
    prop: 'reqDate',
    change: true
  },
  {
    label: '采购备注',
    prop: 'purMemo'
  },
  {
    label: '采购员',
    prop: 'sysUser',
    dbClick: true,
    change: true
  }
])

// const findClicked = (click) => {
//   console.log(click);

//   if (click === '进入订购程序') {
//     Ordering.value = !Ordering.value
//     console.log(Ordering.value);

//   }
// }
</script>

<style lang="less" scoped>
.main {
  width: 100%;
  background-color: white;
}

:deep(.el-select) {
  width: 200px;
}

.btns {
  display: flex;
  padding: 10px 35px 5px;
  justify-content: end;

  .bg {
    background-color: #e7e7e7;
    border: 1px solid #e7e7e7;
    color: #000000;
  }
}

.divNitive {
  :deep(th:nth-child(11)) {
    color: red !important;
  }

  :deep(th:nth-child(13)) {
    color: red !important;
  }

  :deep(th:nth-child(16)) {
    color: red !important;
  }

  :deep(th:nth-child(8)) {
    color: #0000ff !important;
  }

  :deep(th:nth-child(10)) {
    color: #0000ff !important;
  }

  :deep(th:nth-child(15)) {
    color: #0000ff !important;
  }

  :deep(th:nth-child(17)) {
    color: #0000ff !important;
  }
}

.red {
  color: #ff0000;
  font-size: 18px !important;
}

.context-menu-area {
  height: 400px;
  border: 1px solid #000;
  position: relative;
}

.custom-context-menu {
  position: absolute;
  background-color: white;
  border: 1px solid #ccc;
  z-index: 1000;
}

.custom-context-menu ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

.custom-context-menu li {
  padding: 10px;
  cursor: pointer;
}

.custom-context-menu li:hover {
  background-color: #f0f0f0;
}
</style>
